#include <stdio.h>
#include <stdlib.h>
#pragma pack(1)
// 注意 C语言结构体 字节对齐问题

typedef struct record_head
{
    int record_id, content_lenth;
};
typedef struct point_record
{
    // int shape_type;
    double x, y;
};
typedef struct record
{
    int r_id, c_len, s_t;
    double x, y;
};

void read_shp()
{
    FILE *shp = fopen("E:\\Desktop\\data_struct\\11GIS\\SHP\\point_test2.shp", "r");
    int file_code = 0, file_len = 0, version = 0, type = 0;
    fread(&file_code, sizeof(int), 1, shp);
    // 大端序转小端序
    //读取5*4个空字节
    int unused;
    for (int i = 0; i < 5; i++)
        fread(&unused, sizeof(int), 1, shp);
    //读取文件长度
    fread(&file_len, sizeof(int), 1, shp);
    //文件版本
    fread(&version, sizeof(int), 1, shp);
    //文件类型
    fread(&type, sizeof(int), 1, shp);
    switch (type)
    {
        //point
    case 1:
        break;
        //line
    case 3:
        break;
        //polygon
    case 5:
        break;
    }
    double xMin, yMin;
    fread(&xMin, sizeof(double), 1, shp);
    fread(&yMin, sizeof(double), 1, shp);

    double xMax, yMax;
    fread(&xMax, sizeof(double), 1, shp);
    fread(&yMax, sizeof(double), 1, shp);

    double *skip = (double *)malloc(sizeof(double) * 4);
    fread(skip, sizeof(double), 4, shp);
    free(skip);
    printf("%d\n%.5lf,%.5lf\n%.5lf,%.5lf\n", type, xMin, yMin, xMax, yMax);
    //文件头读取结束
    // record rd;
    // fread(&rd, sizeof(rd), 1, shp);
    // printf("\n\n%lf %lf\n\n", rd.x, rd.y);

    // 读取点文件
    while (!feof(shp))
    {
        record_head h;
        fread(&h, sizeof(record_head), 1, shp);
        int shape_type;
        fread(&shape_type, sizeof(int), 1, shp);
        point_record r;
        // if (fread(&r, sizeof(point_record), 1, shp))
        //     printf("%lf %lf\n", r.x, r.y);
        if (fread(&r, sizeof(point_record), 1, shp))
            printf("%lf %lf\n", r.x, r.y);
    }
}

int main()
{
    /*******************code*******************/
    read_shp();
    /******************************************/
    fflush(stdin);

    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
